<?php // $Id: chat.php 11215 2007-02-26 10:29:11Z elixir_julian $
/*
==============================================================================
	Dokeos - elearning and course management software

	Copyright (c) 2004 Dokeos S.A.
	Copyright (c) 2003 Ghent University (UGent)
	Copyright (c) 2001 Universite catholique de Louvain (UCL)
	Copyright (c) Olivier Brouckaert

	For a full list of contributors, see "credits.txt".
	The full license can be read in "license.txt".

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	See the GNU General Public License for more details.

	Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
==============================================================================
*/
/**
==============================================================================
*	Frameset of the Chat tool
*
*	@author Olivier Brouckaert
*	@package dokeos.chat
==============================================================================
*/

$language_file = array ('chat');
include('../inc/global.inc.php');
$nameTools=get_lang('Chat');
//if ($_GET["origin"] != 'whoisonline') {
//	api_protect_course_script();
//}
//else
//{
	$origin = $_SESSION['origin'];
	$target = $_SESSION['target'];
	$_SESSION['origin']=$_GET["origin"];
	$_SESSION['target']=$_GET["target"];
//}

// Connected?
if(isset($_GET['id']))
{
	$chatroomId = $_GET['id'];
	
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $charset;?>">
<title>Chat</title>

<link rel="stylesheet" type="text/css" href="../css/<?php if(api_get_setting('stylesheets')=="") echo 'default'; else echo api_get_setting('stylesheets'); ?>/default.css">

<style>
	a{
		font-size: 12px;
	}
	
	.background_submit{
		background: url(../img/chat_little.gif) 2px 2px no-repeat;
		padding: 2px 1px 1px 20px;
	}
	TH{
		font-size: 12px;
	}
</style>
</head>
<body <?php echo $bodyXtra; ?> >
<h1>Invite to chat room</h1>
<?php
// Get all users
if(isset($_GET['id']))
{
	$chatroomId = $_GET['id'];
	if(isset($_GET['userid'])) 
	{
		$userId = intval($_GET['userid']);
		// Have been invited?
		$table = Database::get_main_table("chatroom_user");
		$query = "SELECT * FROM $table WHERE chatroom_id = $chatroomId AND user_id = $userId";
		$result = api_sql_query($query, __FILE__, __LINE__);
		if($result && mysql_num_rows($result) > 0)
		{
			// Last connected 5 minutes ago?
			$row = mysql_fetch_array($result);
			$lastConnected = strtotime($row['last_connection']);
			if(time() - $lastConnected > 5 * 60)
			{
				// Update table
				$query = "UPDATE $table SET invite_date = NOW(), accepted = 'n' WHERE chatroom_id = $chatroomId AND user_id = $userId";
				api_sql_query($query, __FILE__, __LINE__);
				echo "<p>User invited. Please wait for his/her acceptance. You may close this window.</p>";
			}
			else
				echo "<p>This user has already been invited. Please wait for his/her acceptance, or try again later.</p>";
		}
		else
		{
			// Haven't been invited
			$query = "INSERT INTO $table(chatroom_id, user_id, invite_date) VALUES($chatroomId, $userId, NOW());";
			api_sql_query($query, __FILE__, __LINE__);
			echo "<p>User invited. Please wait for his/her acceptance. You may close this window.</p>";
		}	
	}
	$user = Database::get_main_table(TABLE_MAIN_USER);
	$chatroomUser = Database::get_main_table("chatroom_user");
	global $_user;
	$query = "SELECT user_id, firstname, lastname FROM $user WHERE NOT user_id = $_user[user_id]";
	if($_POST['btnSearch'])
	{
		$query .= " AND (firstname LIKE '%$firstName%' OR lastname LIKE '%$lastName%')";
	}
	$query .= ";";
	//echo $query;
	$result = api_sql_query($query,__FILE__,__LINE__);
	if($result && mysql_num_rows($result) > 0)
	{
		echo "<table>";
		echo "<tr>
			<th>Name</th>
			<th>Action</th>
		</tr>";
		while($row = mysql_fetch_array($result))
		{
			echo "<tr>
				<td>$row[firstname] $row[lastname]</td>
				<td><a href=\"?id=$chatroomId&userid=$row[user_id]\">Invite</a></td>
			</tr>
			";
		}
		echo "</table>";
	}
	else
	{
		echo "<p>There's no users to invite.</p>";
	}
}
?>
</body>
</html>
